package com.qf.xiaomi.service.impl;

import com.qf.xiaomi.dao.GoodsDao;
import com.qf.xiaomi.dao.cartDao;
import com.qf.xiaomi.dao.impl.GoodsDaoImpl;
import com.qf.xiaomi.dao.impl.cartDaoImpl;
import com.qf.xiaomi.pojo.TbCart;
import com.qf.xiaomi.pojo.TbCartList;
import com.qf.xiaomi.pojo.TbGoods;
import com.qf.xiaomi.service.cartService;

import java.util.ArrayList;
import java.util.List;

public class cartServiceImpl implements cartService {
         cartDao dao=new cartDaoImpl();
         GoodsDao Gdao=new GoodsDaoImpl();
    @Override
    public void addCart(long userId, long goodId) {
        dao.addCart(userId,goodId);
    }

    @Override
    public TbCartList selectByUid(long uid, TbCartList tbCartList) {

        List<TbCart> tbCarts = dao.selectCartByUid(uid, tbCartList);

        List ids = new ArrayList();
        long[] nums=new long[tbCarts.size()];
        for (int i = 0; i < tbCarts.size(); i++) {
            ids.add(tbCarts.get(i).getPid());
            nums[i]=tbCarts.get(i).getNum();
        }
        tbCartList.setNums(nums);
        List<TbGoods> goodsList = Gdao.selectByGoodsIds(ids);
        tbCartList.setGoodsList(goodsList);
        return tbCartList;
    }

    @Override
    public void del(long cid) {
        dao.del(cid);
    }

    @Override
    public int clearCart() {
        return dao.clearCart();
    }
}
